<%@ page contentType="text/html;charset=utf-8" %>
<%@ page import="java.util.Date" %>
<%@ page import="ut.data.User" %>
<%@ page import="ut.data.FreeTask" %>
<%@ page import="ut.res.Language" %>
<%@ page import="ut.util.DateTime" %>
<%
	User user = (User)session.getAttribute("user");
	
	int uid, p;
	try{
		uid = Integer.parseInt(request.getParameter("uid"));
	}catch(NumberFormatException e){
		response.sendRedirect("/");
		return;
	}
	try{
		p = Integer.parseInt(request.getParameter("p"));
	}catch(NumberFormatException e){
		p = 1;
	}
	String type = request.getParameter("type");
	if(type == null || (!type.equals("employer") && !type.equals("translator"))){
		if(user != null && user.id == uid){
			response.sendRedirect("/account/user-info.jsp");
		}else{
			response.sendRedirect("/account/member-info.jsp?uid=" + uid);
		}
		return;
	}
	
	User member = User.getUser(uid);
	if(member == null){
		response.sendRedirect("/");
		return;
	}
	
	String subtitle = type.equals("employer") ? "免費發案列表－" + member.name : "免費接案列表－" + member.name;
	
	FreeTask[] tasks = null;
	int maxPage = 1;
	if(type.equals("employer")){
		tasks = FreeTask.getFreeTasks(uid, 10, p);	
		maxPage = FreeTask.getMaxFreeTaskPage(uid, 10);
	}else{
		tasks = FreeTask.getFreeTasks(member, 10, p);
		maxPage = FreeTask.getMaxFreeTaskPage(member, 10);
	}
	if(p > maxPage){
		p = maxPage;
	}
%>

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>UT終極一翻！ | <%= subtitle %></title>
	<link rel="stylesheet" type="text/css" href="/style.css" media="all" />
	<link rel="stylesheet" type="text/css" href="/css/task-list.css" media="all" />
	<script type="text/javascript" src="/js/Utility.js"></script>
	<script type="text/javascript" src="/js/Message.js"></script>
	<script type="text/javascript" src="/js/Ajax.js"></script>
	<script type="text/javascript" src="/js/Button.js"></script>
	<script type="text/javascript" src="/js/Account.js"></script>
	<script type="text/javascript" src="/js/FreeTask.js"></script>
	<script type="text/javascript" src="/script.js"></script>
</head>
<body onload="init();">
<%@ include file="/include/banner.jsp" %>
	<div class="content">
		<div class="title"><%= subtitle %></div>
		<div class="page">
<%
	StringBuilder pageOptions = new StringBuilder("");
	if(p > 1){
		pageOptions.append("<a class=\"page-link\" href=\"./?page=1\">第一頁</a>");
		pageOptions.append("<a class=\"page-link\" href=\"./?page=" + (p-1) + "\">上一頁</a>");
	}else{
		pageOptions.append("<span class=\"page-link inactive\">第一頁</span><span class=\"page-link inactive\">上一頁</span>");
	}
	int startPage = (p - 2 > 0) ? p - 2 : 1;
	int endPage = startPage + 4;
	if(endPage > maxPage){
		startPage = startPage - (endPage - maxPage);
		endPage = maxPage;
	}
	for(int i = startPage; i <= endPage; i++){
		if(i < 1){
			continue;
		}else if(i == p){
			pageOptions.append("<span class=\"page-link current\">" + i + "</span>");
		}else{
			pageOptions.append("<a class=\"page-link\" href=\"./?page=" + i + "\">" + i + "</a>");
		}
	}
	if(p < maxPage){
		pageOptions.append("<a class=\"page-link\" href=\"./?page=" + (p+1) + "\">下一頁</a>");
		pageOptions.append("<a class=\"page-link\" href=\"./?page=" + maxPage + "\">最後頁</a>");
	}else{
		pageOptions.append("<span class=\"page-link inactive\">下一頁</span><span class=\"page-link inactive\">最後頁</span>");
	}
	out.print(pageOptions.toString());
	out.print("</div>");
	
	//案件列表
	if(tasks[0] == null){
%>
		<div class="list block">
			<div class="block-center">目前沒有任何案件</div>
		</div>
<%
	}else{
%>
		<div class="list">
			<table cellspacing="0" cellpadding="5">
				<tr>
					<td class="free title">案件原文</td>
					<td class="free title">案件狀態</td>
					<td class="free title">案主</td>
					<td class="free title">發案日期</td>
					<td class="free title">譯文數</td>
				</tr>
<%
		Date now = new Date();
		String lang = null;
		for(int i = 0; i < tasks.length; i++){
			if(tasks[i] == null){
				break;
			}
%>
				<tr class="free" onclick="document.location.href='/free-task/free-task-detail.jsp?tid=<%= tasks[i].id %>'">
					<td class="content">
						<div style="text-align:left">
							<span class="free tag">
								<%= Language.getLanguage(tasks[i].fromLan).title %>
								<img src="/image/arrow.png" width="10px">
								<%= Language.getLanguage(tasks[i].toLan).title %>
							</span>
						</div>
						<div class="div-content"><%= tasks[i].content %></div>
					</td>
<%
			if(tasks[i].finishTime.before(now)){
%>
					<td class="expired">已結案</td>
<%
			}else{
%>
					<td class="mark-green">開放翻譯</td>
<%
			}
%>
					<td>
						<a href="/account/member-info.jsp?uid=<%= tasks[i].employer %>&view=1"><%= User.getUserName(tasks[i].employer) %></a>
					</td>
					<td><%= DateTime.toDate(tasks[i].publishTime) %></td>
					<td><%= tasks[i].responseNumber %></td>
				</tr>
<%
			if(type.equals("translator")){
				String rid = "task-response-" + i;
%>
				<tr id="<%= rid %>" style="display:none">
					<td colspan="5" class="response"><%= tasks[i].relatedResponse.content %></td>
				</tr>
				<tr>
					<td id="<%= i %>" colspan="5" class="expand" onclick="FreeTask.showResponse(this, <%= i %>)">
						▼ 展開 <a href="/account/member-info.jsp?uid=<%= member.id %>"><%= member.name %></a> 的翻譯  ▼
					</td>
				</tr>
<%
			}
		}
%>
			</table>
		</div>
<%
	}
%>
		<div class="page">
<%
	out.print(pageOptions.toString());
%>
		</div>
<%@ include file="/include/foot.jsp" %>
	</div>
</body>
</html>